package org.mx.dal.config;

import org.mx.dal.service.GeneralAccessor;
import org.mx.dal.service.GeneralDictAccessor;
import org.mx.dal.service.impl.GeneralAccessorElasticImpl;
import org.mx.dal.service.impl.GeneralDictAccessorElasticImpl;
import org.mx.dal.utils.ElasticConfigBean;
import org.mx.dal.utils.ElasticUtil;
import org.mx.dal.utils.impl.ElasticUtilImpl;
import org.mx.spring.session.SessionDataStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * 描述： 基于Elasticsearch的DAL基础实现
 *
 * @author John.Peng Date time 2018/4/1 上午8:40
 */
@Import(DalConfig.class)
public class DalElasticConfig {
	/**
	 * 创建Elastic配置对象
	 * 
	 * @return 配置对象
	 */
	@ConditionalOnProperty(prefix = "elastic", name = "enabled", havingValue = "true")
	@Bean(name = "elasticConfigBean")
	public ElasticConfigBean elasticConfigBean() {
		return new ElasticConfigBean();
	}

	/**
	 * 创建基于Elastic的REST服务工具
	 * 
	 * @param elasticConfigBean 配置对象
	 * @return Elastic服务工具
	 */
	@ConditionalOnBean(ElasticConfigBean.class)
	@Bean(name = "elasticUtil", initMethod = "init", destroyMethod = "destroy")
	public ElasticUtil elasticUtils(ElasticConfigBean elasticConfigBean) {
		return new ElasticUtilImpl(elasticConfigBean);
	}

	/**
	 * 创建基于Elastic的数据对象Accessor
	 * 
	 * @param sessionDataStore 会话数据存储
	 * @param elasticUtil      Elastic工具
	 * @return Accessor
	 */
	@ConditionalOnBean(ElasticUtil.class)
	@Bean(name = "generalAccessorElastic")
	public GeneralAccessor generalAccessorElastic(SessionDataStore sessionDataStore, ElasticUtil elasticUtil) {
		return new GeneralAccessorElasticImpl(sessionDataStore, elasticUtil);
	}

	/**
	 * 创建基于Elastic的字典对象Accessor
	 * 
	 * @param sessionDataStore 会话数据存储
	 * @param elasticUtil      Elastic工具
	 * @return Accessor
	 */
	@ConditionalOnBean(ElasticUtil.class)
	@Bean(name = "generalDictAccessorElastic")
	public GeneralDictAccessor generalDictAccessorElastic(SessionDataStore sessionDataStore, ElasticUtil elasticUtil) {
		return new GeneralDictAccessorElasticImpl(sessionDataStore, elasticUtil);
	}
}
